package com.rfsp.mcp.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rfsp.common.util.StringUtil;
import com.rfsp.mcp.mapper.SystemCodeMapper;

@Service
public class SystemCodeService {
	
	@Autowired
	SystemCodeMapper systemCodeMapper;
	
	public String getBussCode(String bussType) {
		String seq = "";
		String type = "";
		
		String datetime =  StringUtil.dateToString(new Date(), "yyyyMMdd");
		Map<String,String> params = new HashMap<String,String>();
		
		
		
		if("finance".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_FINANCE_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "MEDI";
		}
		
		if("auth".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_AUTH_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "AU";
		}
		if("auth-pfm".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_AUTH_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "GAU";
		}
		if("bargain".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_BARGAIN_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "FIN";
		}
		if("mi".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_MI_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "REG";
		}
		if("mi-pfm".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_MI_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "GREG";
		}
		if("payLog".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_MI_PAY_LOG_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "PL";
		}
		if("payLog-pfm".equals(bussType)) {
			params.put("sequenceName", "SEQ_MCP_MI_PAY_LOG_NO");
			seq = systemCodeMapper.getSequence(params).getSequenceNo();
			type = "GPL";
		}
		return type+datetime+fillZero(8,seq);
	}
	
	/**
	 * 字符串长度不够，补0的方法
	 * @param maxlen
	 * @param str
	 * @return
	 * @throws IllegalArgumentException
	 */
	private String fillZero(int maxlen, String str)
			throws IllegalArgumentException {
		if (str == null) {
			String msg = "传入的参数为空 ";
			throw new IllegalArgumentException(msg);
		}
		int size = str.length();
		if (size == 0 || size > maxlen) {
			String msg = "传入参数的长度" + size + "不合法 ";
			throw new IllegalArgumentException(msg);
		}
		String zero = "0";
		for (int i = size; i < maxlen; i++)
			str = zero.concat(str);
		return str;
	}

}
